package scau.mis4.chanyulin.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import scau.mis4.chanyulin.base.common.BaseController;
import scau.mis4.chanyulin.entity.OrderItem;
import scau.mis4.chanyulin.service.IOrderItemService;
import java.util.List;

/**
 * Created by cyl on 2018/03/27.
 * OrderItem Controller层
 */
@RestController
@RequestMapping("/orderitem")
public class OrderItemController extends BaseController {

    @Autowired
    private IOrderItemService orderItemService;

    /**
     * 添加OrderItem
     * @param orderItem
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    @RequiresRoles(value={"0","1", "2"},logical = Logical.OR)
    public String addOrderItem(OrderItem orderItem) throws Exception {
        orderItemService.add(orderItem);
        return createSuccessJSON();
    }

    /**
     * 删除某个OrderItem
     * @param id
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    @RequiresRoles(value={"0","1", "2"},logical = Logical.OR)
    public String deleteOrderItemById(String id) throws Exception {
        orderItemService.delete(id);
        return createSuccessJSON();
    }

    /**
     * 通过id查找某个OrderItem
     * @param id
     * @return
     * @throws Exception
     */
    @GetMapping("/get_by_id")
    public String getOrderItemById(String id) throws Exception {
        OrderItem orderItem = orderItemService.findByKey(id);
        Model model = new ExtendedModelMap().addAttribute("result", orderItem);
        return createSuccessJSON(model);
    }

    /**
     * 通过orderTotId查找某个OrderItem
     * @param orderTotId
     * @return
     * @throws Exception
     */
    @GetMapping("/get_by_ordertot_id")
    public String getOrderItemByOrderTotId(String orderTotId) throws Exception {
        List<OrderItem> orderItemList = orderItemService.findByProperties(orderTotId, "orderTotId");
        Model model = new ExtendedModelMap().addAttribute("result", orderItemList);
        return createSuccessJSON(model);
    }

    /**
     * 获取全部OrderItem
     * @return
     * @throws Exception
     */
    @GetMapping("/list")
    public String listAllOrderItem() throws Exception {
        List<OrderItem> orderItemList = orderItemService.findAll();
        Model model = new ExtendedModelMap().addAttribute("result",orderItemList);
        return createSuccessJSON(model);
    }

    /**
     * 分页查询OrderItem
     * @param pageNum 页码
     * @param pageSize 每页数据数量
     * @return
     * @throws Exception
     */
    @PostMapping("/page")
    public String listOrderItemByPage(int pageNum, int pageSize) throws Exception {
        PageHelper.startPage(pageNum, pageSize);
        List<OrderItem> orderItemList = orderItemService.findAll();
        PageInfo<OrderItem> pageInfo = new PageInfo<>(orderItemList);
        Model model = new ExtendedModelMap()
                .addAttribute("result", orderItemList)
                .addAttribute("pageInfo", pageInfo);
        return createSuccessJSON(model);
    }

    /**
     * 更新某个OrderItem
     * @param orderItem
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    @RequiresRoles(value={"0","1", "2"},logical = Logical.OR)
    public String updateOrderItemById(OrderItem orderItem) throws Exception {
        orderItemService.updateById(orderItem);
        return createSuccessJSON();
    }

    /**
     * 退货
     * @param id
     * @return
     * @throws Exception
     */
    @PostMapping("/return")
    @RequiresRoles(value={"0","1", "2"},logical = Logical.OR)
    public String returnGood(String id) throws Exception {
        OrderItem orderItem = orderItemService.findByKey(id);
        orderItem.setStatus("已退货");
        return createJSONByUpdates(orderItemService.update(orderItem));
    }

}
